package com.geekaca.testthread.threadpool77;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class ThreadPoolDemo3 {
    public static void main(String[] args) {
        /**
         * 线程池简便创建方式
         */
        // 只要不够就创建,容易把内存涨满
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new ZuocaiRunnable());
        executorService.execute(new ZuocaiRunnable());
        executorService.execute(new ZuocaiRunnable());
        executorService.execute(new ZuocaiRunnable());
        executorService.execute(new ZuocaiRunnable());
        executorService.execute(new ZuocaiRunnable());

        Executors.newFixedThreadPool(5);
        // 固定数量的线程池, 任务队列没有长度限制, 队列容易涨满
        executorService.execute(new ZuocaiRunnable());
        executorService.execute(new ZuocaiRunnable());
        executorService.execute(new ZuocaiRunnable());
        // 池中只有一个线程,但是队列无限制
        ExecutorService executorServiceSingle = Executors.newSingleThreadExecutor();
        executorServiceSingle.execute(new ZuocaiRunnable());
        executorServiceSingle.execute(new ZuocaiRunnable());
        executorServiceSingle.execute(new ZuocaiRunnable());

        // 定时器
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
        scheduledExecutorService.scheduleAtFixedRate(new TestRunnable(),5,1, TimeUnit.SECONDS);
    }

}

class TestRunnable implements   Runnable{
    @Override
    public void run() {
        System.out.println("Hello:"+System.currentTimeMillis()  );

    }
}
